//
//  BVSearchViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/4.
//  Copyright © 2016年 com.yinding. All rights reserved.
//  搜索界面

#import "BVSearchViewController.h"
#import "BVSearchBar.h"
#import "BVSearchView.h"
#import "BVSearch.h"
#import "BVNavItem.h"
#define HISTORY_SEARCH @"historys"

@interface BVSearchViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) BVSearchBar *searchBar;
@property (nonatomic, strong) BVSearchView *searchView;
@property (nonatomic, strong) BVSearch *search;
@property (nonatomic, strong) BVNavItem *navItem;

@end

@implementation BVSearchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.searchBar becomeFirstResponder];
    self.searchView.dataSource = [self.search fetchSearchText];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.searchBar = [[BVSearchBar alloc] init];
    self.searchBar.frame = CGRectMake(0, 0, ScreenW - 70, 30);
    self.searchBar.delegate = self;
    self.searchBar.returnKeyType = UIReturnKeySearch;
    self.navigationItem.titleView = self.searchBar;
    [self.view addSubview:self.searchView];
    
    self.navItem = [[BVNavItem alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [self.navItem setTitle:@"取消" forState:UIControlStateNormal];
    [self.navItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.navItem addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -10;
    self.navigationItem.rightBarButtonItems = @[space,[[UIBarButtonItem alloc] initWithCustomView:self.navItem]];
    
}
- (void)cancelClick {
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark -
#pragma mark - ReturnClickAction
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField.text isEqualToString:@""]) {
        return NO;
    }
    [self.searchBar resignFirstResponder];
    [self saveSearchText:textField.text];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.searchBar resignFirstResponder];
}

#pragma mark -
#pragma mark - 保存搜索数据
- (void)saveSearchText:(NSString *)searchText {
    [self.search saveSearchText:searchText];
    [self.searchView reloadData];
    self.searchBar.text = nil;
}

- (BVSearch *)search {
    if (!_search) {
        _search = [[BVSearch alloc] init];
    }
    return _search;
}

- (BVSearchView *)searchView {
    if (!_searchView) {
        _searchView = [[BVSearchView alloc] init];
        _searchView.frame = self.view.frame;
        __weak __typeof(self) weakSelf = self;
        _searchView.deleteBlock = ^{
            [weakSelf showAlert];
        };
//        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] init];
//        [tapGes addTarget:self action:@selector(tap:)];
//        _searchView.userInteractionEnabled = YES;
//        [_searchView addGestureRecognizer:tapGes];
    }
    return _searchView;
}

- (void)showAlert {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定删除所有搜索记录吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    UIAlertAction *certainAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.search deleteAllSearchText];
        /*刷新数据源*/
        self.searchView.dataSource = [self.search fetchSearchText];
        [self.searchView reloadData];
    }];
    [alert addAction:cancelAction];
    [alert addAction:certainAction];
    [self presentViewController:alert animated:YES completion:nil];
}

//- (void)tap:(UITapGestureRecognizer *)tap {
//    [self.searchBar resignFirstResponder];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
